﻿<div class="ph-tabs">
    <ul>
        <li class="k-state-active">班级列表</li>
        <li>批量导入班级</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <span class="k-textbox k-space-right">
                <input type="text" placeholder="请输入班级名称" id="txtKey" />
                <a href="javascript:FilterClass()" class="k-icon k-i-search">&nbsp;</a>
            </span>
            <button class="k-button" onclick="CreateNewClass()"><span class="k-icon k-i-plus"></span><span>添加新班级</span></button>
        </div>
        <div id="grid_class" style="text-align: center"></div>
    </div>
    <div>
        <div class="GeneralEdit" id="fmStudentImport">
            <ul>
                <li>
                    <label>模板：</label>
                    <a href="/School/Class/ExportTemplate" target="_blank" class="k-button">点击下载班级导入模板</a>
                </li>
                <li>
                    <label>上传文件：</label>
                    <ul class="upfile k-block">
                        <li>
                            <input type="file" id="fileAux" name="fileAux" data-fid="" /></li>
                        <li>
                            <h4 id="fuAuxMsg"></h4>
                        </li>
                    </ul>
                </li>
                <li id="lip">
                    <label>完成进度：</label>
                    <div id="progressbar"></div>
                </li>

                <li class="editButtons">
                    <button id="btnImport" class="k-button"><span class="k-icon k-i-tick"></span><span>开始导入</span></button>
                </li>
            </ul>
        </div>
    </div>
</div>

<script class="init">
    LocatedMenu("学校管理", "班级管理");
</script>

<script class="grid_class_columns">
    var classFields = [{
        title: "班级编号",
        field: "CIndex",
        template: "#=GradeCode##=kendo.toString(CIndex,'00')#",
        width: 80
    }, {
        title: "班级名称",
        field: "ClassName",
        width: 150
    }, {
        title: "年级",
        field: "GradeName",
        width: 150
    }, {
        title: "班主任",
        field: "Advisor",
        width: 100
    }, {
        title: "人数",
        field: "PersonCount",
        width: 50
    }, {
        title: "添加时间",
        template: "#=kendo.toString(kendo.parseDate(CreatedOn),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "操作",
        template: "#=GetOperations(Guid)#",
        attributes: { style: "text-align:left" }
    }];

    function GetOperations(guid) {
        var html = "<button class='k-button' onclick=\"EditClass('" + guid + "')\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"DeleteClass('" + guid + "')\"><span class='k-icon k-i-cancel'></span><span>删除</span></button>";
        html += "<button class='k-button' onclick=\"RemoveStudents('" + guid + "')\"><span class='k-icon k-i-close'></span><span>清空学生</span></button>";
        return html;
    }

    function EditClass(guid) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改班级信息" });
            wndGeneralEdit.refresh({ url: "/School/Class/Edit/" + guid });
            wndGeneralEdit.open();
        }
    }

    function DeleteClass(guid) {
        if (confirm("真的要删除这个班级吗？")) {
            Whale.Remote.Operation({
                url: "/School/Class/DeleteClass",
                parameters: { guid: guid },
                success: function (result) {
                    if ($("#grid_class").length > 0)
                        $("#grid_class").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }

    function RemoveStudents(guid) {
        if (confirm("真的要清除这个班级的学生吗？")) {
            Whale.Remote.Operation({
                url: "/School/Student/RemoveStudent",
                parameters: { classGuid: guid },
                success: function (result) {
                    if ($("#grid_class").length > 0)
                        $("#grid_class").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_class").kendoGrid({
            columns: classFields,
            dataSource: Whale.dataSource({
                url: "/School/Class/GetClasses",
                getParameters: function () {
                    return {
                        schoolGuid: "@ViewBag.SchoolGuid",
                        key: $.trim($("#txtKey").val()),
                        getPersonCount: true,
                        getGradeName:true
                    };
                },
                pageSize: 15

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function CreateNewClass() {
        wndGeneralEdit.setOptions({ title: "添加新的班级" });
        wndGeneralEdit.refresh({ url: "/School/Class/Edit" });
        wndGeneralEdit.open();
    }

    function FilterClass() {
        $("#grid_class").data("kendoGrid").dataSource.page(0);
    }
</script>

<script class="import_class">
    var pb = $("#progressbar").kendoProgressBar({
        type: "percent",
        min: 0,
        max: 100
    }).data("kendoProgressBar");
    $("#lip").hide();
    $(function () {
        $("#btnImport").click(function (e) {
            var values = {
                fGuid: $("#fileAux").attr("data-fid"),
                sGuid: "@ViewBag.SchoolGuid",
            };

            Whale.Remote.Operation({
                url: "/School/Class/ImportClass",
                parameters: values,
                success: function (result) {
                    FilterClass();
                    $(".ph-tabs").data("kendoTabStrip").select(0);
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                    FilterClass();
                }
            });

            $("#btnImport").hide();
            $("#lip").show();
            setTimeout(function () {
                GetFileProgress(values.fGuid, pb);
            }, 3000);
        });
    });
</script>

